package com.example.chat.mapper;

import com.example.chat.pojo.User;
import org.apache.ibatis.annotations.*;

@Mapper
public interface UserMapper {

        // 根据用户名查询用户
        @Select("SELECT * FROM user WHERE username = #{username}")
        User findByUserName(String username);

        // 插入新用户
        @Insert("INSERT INTO user(username, password, email, tokens) VALUES(#{username}, #{password}, #{email}, #{tokens})")
        void add(User user);

        // 更新用户的验证码和生成时间
        @Update("UPDATE user SET verificationCode = #{verificationCode}, codeGeneratedTime = #{codeGeneratedTime} WHERE email = #{email}")
        void updateVerificationCode(@Param("email") String email,
                                    @Param("verificationCode") String verificationCode,
                                    @Param("codeGeneratedTime") String codeGeneratedTime);

        // 根据邮箱查询用户
        @Select("SELECT * FROM user WHERE email = #{email}")
        User findByEmail(String email);

        // 根据邮箱查询用户的验证码
        @Select("SELECT verificationCode FROM user WHERE email = #{email} AND codeGeneratedTime + 600000 > #{currentTime}")
        String findVerificationCodeByEmail(@Param("email") String email, @Param("currentTime") long currentTime);

        // 激活用户
        @Update("UPDATE user SET status = '已激活' WHERE username = #{username}")
        void activateUser(@Param("username") String username);

        // 更新用户的 tokens 值
        @Update("UPDATE user SET tokens = #{tokens} WHERE id = #{id}")
        void updateUserTokens(User user);
}
